package com.open.capacity.reimburse.service.impl;

import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.reimburse.entity.PbdBudgetAdjust1;
import com.open.capacity.utils.SysUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.*;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.PbdBudgetAdjust2;
import com.open.capacity.reimburse.dao.PbdBudgetAdjust2Dao;
import com.open.capacity.reimburse.service.PbdBudgetAdjust2Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class PbdBudgetAdjust2ServiceImpl implements PbdBudgetAdjust2Service {

    @Autowired
    private PbdBudgetAdjust2Dao pbdBudgetAdjust2Dao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveOrUpdate(Integer year, String projectId, String departmentId, PbdBudgetAdjust2[] list) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();
        pbdBudgetAdjust2Dao.deleteByDepartmentId(year, projectId, departmentId);
        for (int i = 0; i < list.length; i++) {
            list[i].setId(UUIDUtils.getOriginalUUIDWithoutBar());
            list[i].setUpdateTime(new Date());
            list[i].setUpdateUserid(loginUser.getId());
            list[i].setUpdateUsername(loginUser.getRealName());
            pbdBudgetAdjust2Dao.save(list[i]);
        }
        return 0;
    }

    /**
     * 添加
     *
     * @param pbdBudgetAdjust2
     */
    public int save(PbdBudgetAdjust2 pbdBudgetAdjust2) {
        pbdBudgetAdjust2.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        pbdBudgetAdjust2.setStatus(1);
        pbdBudgetAdjust2.setUpdateTime(new Date());
        return pbdBudgetAdjust2Dao.save(pbdBudgetAdjust2);
    }

    /**
     * 修改
     *
     * @param pbdBudgetAdjust2
     */
    public int update(PbdBudgetAdjust2 pbdBudgetAdjust2) {
        pbdBudgetAdjust2.setUpdateTime(new Date());
        return pbdBudgetAdjust2Dao.update(pbdBudgetAdjust2);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return pbdBudgetAdjust2Dao.delete(id);
    }

    @Override
    public List<PbdBudgetAdjust2> findByDepartmentId(String projectId, String departmentId) {
        Map<String, Object> params = new HashMap<>();
        params.put("projectId", projectId);
        params.put("departmentId", departmentId);
        return pbdBudgetAdjust2Dao.findAll(params);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public PbdBudgetAdjust2 findById(String id) {
        return pbdBudgetAdjust2Dao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<PbdBudgetAdjust2> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<PbdBudgetAdjust2> list = pbdBudgetAdjust2Dao.findAll(params);
        PageInfo<PbdBudgetAdjust2> pageInfo = new PageInfo(list);

        return PageResult.<PbdBudgetAdjust2>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<PbdBudgetAdjust2> findAll(Map<String, Object> params) {
        return pbdBudgetAdjust2Dao.findAll(params);
    }

}
